package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.ApplyBase;
import com.ipfsemc.ipfsemc.model.UserSpace;
import com.ipfsemc.ipfsemc.model.UserSpaceVo;
import com.ipfsemc.ipfsemc.service.IApplyService;
import com.ipfsemc.ipfsemc.service.IUserSpaceService;
import com.ipfsemc.ipfsemc.util.JsonData;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.util.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("userSpace")
@Api(value = "UserSpaceController", tags = "用户矿机")

public class UserSpaceController {
    private static final Logger logger = LoggerFactory.getLogger(ApplyController.class);
    @Autowired
    private IUserSpaceService userSpaceService;

    @Autowired
    private IApplyService applyService;

    @ApiOperation(value = "我空间的矿机",httpMethod = "GET")
    @RequestMapping(value = "listUserSpace",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    public JsonData listUserSpace(String userId){
        JsonData jsonData=new JsonData();
        try {
            if (null == userId) {
                jsonData.setMessage("参数有误");
            } else {
                UserSpaceVo userSpace=new UserSpaceVo();
                userSpace.setUserId(userId);
                userSpace.setSpaceId(1L);
                List<UserSpaceVo> userSpacesOne = userSpaceService.listByUser(userSpace);
                List<UserSpaceVo> userSpacesTwo = userSpaceService.listOut(userSpace);
                List<UserSpaceVo> userSpaceVoThree = userSpaceService.listOut7Day(userSpace);

                //退租中
                userSpace.setSpaceState(1);
                userSpace.setProType(2);
                List<UserSpaceVo> outZu = userSpaceService.listOutZu(userSpace);
                //转让中
                userSpace.setProType(3);
                List<UserSpaceVo> outZhuan = userSpaceService.listOutZu(userSpace);
                //以退租
                userSpace.setSpaceState(-1);
                userSpace.setProType(2);
                List<UserSpaceVo> zuOk = userSpaceService.listOutZu(userSpace);
                //以转让
                userSpace.setProType(3);
                List<UserSpaceVo> ZhuanOk = userSpaceService.listOutZu(userSpace);

                //已过期7天
                jsonData.put("out7DayResult",userSpaceVoThree);
//
//                //已过期
                jsonData.put("outResult",userSpacesTwo);
//                //正常
                jsonData.put("userResult",userSpacesOne);
//
                jsonData.put("outZu",outZu);
                jsonData.put("outZhuan",outZhuan);
                jsonData.put("zuOk",zuOk);
                jsonData.put("ZhuanOk",ZhuanOk);
                jsonData.setCode(0);
            }
        } catch (Exception ex) {
            logger.error("[UserSpace][listUserSpace]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }



    @ApiOperation(value = "矿机转让、退租",tags = "海王退租或者是海皇转让",httpMethod = "POST")
    @RequestMapping(value = "applyUpdate",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID  矿机转让时的用户Id",required=false,dataType="String"),
            @ApiImplicitParam(name="spaceId",value="矿机",required=true,dataType="Long")
    })
    public ResponseBase applyUpdate(String userId, Long spaceId){
        ResponseBase jsonData=new ResponseBase();
        try {
            if (null == spaceId) {
                jsonData.setMessage("参数有误");
            } else {
                UserSpace userSpace=new UserSpace();
                userSpace.setUserId(userId);
                userSpace.setSpaceId(spaceId);
                int i = userSpaceService.applyUpdate(userSpace);
                if (i<0){

                    jsonData.setMessage("申请失败！请稍后再试！");
                }else {
                    jsonData.setCode(0);
                }
            }
        } catch (Exception ex) {

            logger.error("[UserSpace][applyUpdate]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "空间",tags = "我空间和伙伴空间总大小算力大小",httpMethod = "GET")
    @RequestMapping(value = "listAll",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    public JsonData listAll(String userId){
        JsonData jsonData=new JsonData();
        try {
            if (null == userId) {
                jsonData.setMessage("参数有误");
            } else {
                UserSpaceVo userSpace=new UserSpaceVo();
                userSpace.setUserId(userId);
                UserSpaceVo myAll = userSpaceService.selectMyAll(userSpace);
                UserSpaceVo firendAll = userSpaceService.selectFirendAll(userSpace);

                jsonData.put("myResult",myAll);
                jsonData.put("firendResult",firendAll);
                jsonData.setCode(0);
            }
        } catch (Exception ex) {

            logger.error("[UserSpace][listAll]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiOperation(value = "你伙伴空间的状态",tags = "你伙伴空间的矿机状态",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    @RequestMapping(value = "listFirend",method = RequestMethod.GET)
    public Response<ApplyBase> listFirend(String userId){
        Response jsonData=new Response();
        try {
            if (null == userId) {
                jsonData.setMessage("参数有误");
            } else {
                List<ApplyBase> applyBases = applyService.FirSpace(userId);
                jsonData.setResult(applyBases);
                jsonData.setCode(0);
            }
        } catch (Exception ex) {

            logger.error("[UserSpace][listFirend]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;

    }

    @ApiOperation(value = "查询单个矿机",httpMethod = "GET")
    @RequestMapping(value = "selectOneMill",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="spaceId",value="矿机",required=true,dataType="Long")
    })
    public Response<UserSpaceVo> selectOneMill(Long spaceId){
        Response jsonData=new Response();
        try {
            if (null == spaceId) {
                jsonData.setMessage("参数有误");
            } else {
                UserSpaceVo userSpace=new UserSpaceVo();
                userSpace.setSpaceId(spaceId);
                UserSpaceVo userSpaceVo = userSpaceService.selectOneMill(userSpace);

                jsonData.setResult(userSpaceVo);
                jsonData.setCode(0);
            }
        } catch (Exception ex) {

            logger.error("[UserSpace][selectOneMill]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;

    }
}
